package com.hong.pdf2img;

import com.hong.pdf2img.utils.AppLogger;
import com.hong.pdf2img.utils.FileDialogUtils;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;
import lombok.Setter;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import static com.hong.pdf2img.MainApplication.showErrorDialog;
import static com.hong.pdf2img.MainApplication.showInfoDialog;

public class TextDialogController {
    @Setter
    private Stage stage; // 存储主舞台的引用
    @Setter
    private TextArea showResultBox;
    @FXML
    private TextArea pdfText;

    @FXML
    void close(ActionEvent event) {

    }

    @FXML
    void saveText(ActionEvent event) {
        AppLogger.info("保存文本");
        File file= FileDialogUtils.showSaveTextDialog(stage, "pdfText.txt",MainApplication.outputPath);
        if (file != null) {
            try {
                String content = pdfText.getText();
                Files.writeString(file.toPath(), content);
                showInfoDialog("文件保存成功: " + file.getName());
                showResultBox.appendText(file.getAbsolutePath());
            } catch (Exception e) {
                showErrorDialog("保存文件时出错: " + e.getMessage());
                AppLogger.error("保存文件时出错", e);
            }
        }
    }
    public void show(){

    }
    public void setText(String text){
        pdfText.setText(text);
    }
}
